package com.sky.service.common.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.common.DishFlavorMapper;
import com.sky.mapper.common.DishMapper;
import com.sky.mapper.common.SetmealMapper;
import com.sky.pojo.Category;
import com.sky.pojo.Dish;
import com.sky.pojo.DishFlavor;
import com.sky.result.PageResult;
import com.sky.service.common.DishService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;


    @Autowired
    private DishFlavorMapper dishFlavorMapper;


    @Autowired
    private SetmealMapper setmealMapper;


    //添加菜品
    @Transactional
    public void saveWithFlavor(DishDTO dishDTO) {

        Dish dish = new Dish();

        BeanUtils.copyProperties(dishDTO, dish);

        //添加菜品
        dishMapper.insert(dish);

        //给菜品的口味添加上菜品id
        Long dishid = dish.getId();
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dishid);
            }
        }
        //向口味表插入n条数据
        dishFlavorMapper.insertBatch(flavors);
    }


    //分页查询
    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> page = dishMapper.pageQuery(dishPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }


    //批量删除菜品
    @Transactional
    @Override
    public void delete(List<Long> ids) {
        //根据菜品id和状态查询菜品数量
        Integer count = dishMapper.selectCountByIdsAndStatus(ids, StatusConstant.ENABLE);
        log.info("count:{}", count);
        //在售状态下不可以删除菜品
        if (count > 0) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
        }
        dishMapper.delete(ids);
    }


    //修改菜品起售和停售
    @Override
    public void startOrStop(Integer status, Integer id) {
        //修改
        dishMapper.startOrStop(status, id);
    }



    //修改菜品信息
    @Override
    public void updateWithFlavor(DishDTO dishDTO) {

        dishMapper.updateWithFlavor(dishDTO);

    }


    //根据id查询菜品
    @Override
    public DishVO findDishById(Long id) {
        DishVO dishVO = dishMapper.findDishById(id);
        return dishVO;
    }


    //根据分类id查询菜品
    @Override
    public List<DishVO> findDishByCategoryId(Long categoryId) {
        List<DishVO> dishByCategoryId = dishMapper.findDishByCategoryId(categoryId);


        return dishByCategoryId;
    }



    //查询菜品分类user
    @Override
    public List<DishVO> findDishByCategory(Long categoryId) {
        List<DishVO> dishVOS = dishMapper.findDishByCategory(categoryId);

        for (DishVO dishVO : dishVOS) {
                  // 根据菜品ID查询对应的口味信息，并将查询结果设置到菜品VO对象中
          List<DishFlavor> flavors = dishMapper.findFlavorByDishId(dishVO.getId());
          dishVO.setFlavors(flavors);

        }
        return dishVOS;
    }


}

